/*
 * DialogoCrearEmpleado.java
 *
 * Created on 14 de junio de 2008, 18:26
 */
package presentacion.vista;

import accesoDatos.dao.DaoEmpleado;
import accesoDatos.dao.DaoPedido;
import accesoDatos.util.Encriptacion;
import accesoDatos.vo.Empleado;
import accesoDatos.vo.Pedido;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import presentacion.util.Ventana;

/**
 *
 * @author  Tatis
 */
public class DialogoCrearEmpleado extends javax.swing.JDialog {

    private Empleado empleado = new Empleado();
    private int mod = 0;//modificar=1, crear=0
    /** Creates new form DialogoCrearEmpleado */
    public DialogoCrearEmpleado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        mod = 0;
        Ventana.centrar(this);
        botonCambiarContraseña.setIcon(new ImageIcon("src/presentacion/vista/iconoTeclado.png"));
    }

    public DialogoCrearEmpleado(java.awt.Frame parent, boolean modal, int idEmp) {
        super(parent, modal);
        mod = 1;
        initComponents();
        Ventana.centrar(this);
        botonCambiarContraseña.setIcon(new ImageIcon("src/presentacion/vista/iconoTeclado.png"));
        empleado = DaoEmpleado.read(idEmp);
        nameField.setText(empleado.getNombre());
        if (empleado.getCargo().equals("Administrador")) {
            campoCargo.addItem("Administrador");
            campoCargo.setEnabled(false);
        }
        campoCargo.setSelectedItem(empleado.getCargo());
        passField.setVisible(false);
        labelPass.setVisible(false);
        telField.setText(empleado.getTelefono());
        celField.setText(empleado.getCelular());
        passField.setText(empleado.getPassword());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelInformacion = new javax.swing.JPanel();
        labelNombre = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        labelCargo = new javax.swing.JLabel();
        campoCargo = new javax.swing.JComboBox();
        labelPass = new javax.swing.JLabel();
        passField = new javax.swing.JTextField();
        telField = new javax.swing.JTextField();
        labelTel = new javax.swing.JLabel();
        celField = new javax.swing.JTextField();
        labelCel = new javax.swing.JLabel();
        botonCancelar = new javax.swing.JButton();
        botonAceptar = new javax.swing.JButton();
        botonCambiarContraseña = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nuevo producto");

        panelInformacion.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion de empleado"));

        labelNombre.setText("Nombre:");

        nameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                nameFieldKeyTyped(evt);
            }
        });

        labelCargo.setText("Cargo:");

        campoCargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mesero", "Cocina", "Cajero" }));

        labelPass.setText("Password:");

        passField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passFieldActionPerformed(evt);
            }
        });
        passField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                passFieldKeyTyped(evt);
            }
        });

        telField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                telFieldKeyTyped(evt);
            }
        });

        labelTel.setText("Teléfono:");

        celField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                celFieldKeyTyped(evt);
            }
        });

        labelCel.setText("Celular");

        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonAceptar.setText("Aceptar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        botonCambiarContraseña.setBorderPainted(false);
        botonCambiarContraseña.setContentAreaFilled(false);
        botonCambiarContraseña.setFocusPainted(false);
        botonCambiarContraseña.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                botonCambiarContraseñabotonMouseExited(evt);
            }
        });
        botonCambiarContraseña.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                botonCambiarContraseñabotonMouseMoved(evt);
            }
        });
        botonCambiarContraseña.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCambiarContraseñaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelInformacionLayout = new javax.swing.GroupLayout(panelInformacion);
        panelInformacion.setLayout(panelInformacionLayout);
        panelInformacionLayout.setHorizontalGroup(
            panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelInformacionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelInformacionLayout.createSequentialGroup()
                        .addComponent(labelNombre)
                        .addGap(26, 26, 26)
                        .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))
                    .addGroup(panelInformacionLayout.createSequentialGroup()
                        .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelCargo)
                            .addComponent(labelPass)
                            .addComponent(labelTel)
                            .addComponent(labelCel))
                        .addGap(17, 17, 17)
                        .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                            .addComponent(campoCargo, 0, 252, Short.MAX_VALUE)
                            .addComponent(telField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                            .addComponent(celField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelInformacionLayout.createSequentialGroup()
                        .addComponent(botonCambiarContraseña)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
                        .addComponent(botonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addContainerGap())
        );
        panelInformacionLayout.setVerticalGroup(
            panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelInformacionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNombre))
                .addGap(18, 18, 18)
                .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCargo))
                .addGap(18, 18, 18)
                .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPass)
                    .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(telField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTel))
                .addGap(18, 18, 18)
                .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(celField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(panelInformacionLayout.createSequentialGroup()
                        .addComponent(botonCambiarContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(panelInformacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonAceptar)
                        .addComponent(botonCancelar))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelInformacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(panelInformacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panelInformacion.getAccessibleContext().setAccessibleName("Informacion del empleado");

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void nameFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameFieldKeyTyped
        if (nameField.getText().length() > 50) {
            evt.consume();
        }
    }//GEN-LAST:event_nameFieldKeyTyped

    private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passFieldKeyTyped

        char caracter = evt.getKeyChar();

        if (passField.getText().length() == 4) {
            evt.consume();
        }

        if ((caracter >= '0') && (caracter <= '9')) {
            return;
        } else {
            evt.consume();
        }        
}//GEN-LAST:event_passFieldKeyTyped

    private void telFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_telFieldKeyTyped
        char caracter = evt.getKeyChar();

        if (telField.getText().length() == 7) {
            evt.consume();
        }

        if ((caracter >= '0') && (caracter <= '9')) {
            return;
        } else {
            evt.consume();
        }
    }//GEN-LAST:event_telFieldKeyTyped

    private void celFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_celFieldKeyTyped
        char caracter = evt.getKeyChar();

        if (celField.getText().length() == 10) {
            evt.consume();
        }

        if ((caracter >= '0') && (caracter <= '9')) {
            return;
        } else {
            evt.consume();
        }
    }//GEN-LAST:event_celFieldKeyTyped

    private void botonCambiarContraseñaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCambiarContraseñaActionPerformed
        TecladoEnPantalla.mostrar();
    }//GEN-LAST:event_botonCambiarContraseñaActionPerformed

    private void botonCambiarContraseñabotonMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonCambiarContraseñabotonMouseMoved
        JButton boton = (JButton) evt.getComponent();
        boton.setContentAreaFilled(true);
        boton.setBorderPainted(true);
    }//GEN-LAST:event_botonCambiarContraseñabotonMouseMoved

    private void botonCambiarContraseñabotonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonCambiarContraseñabotonMouseExited
        JButton boton = (JButton) evt.getComponent();
        boton.setContentAreaFilled(false);
        boton.setBorderPainted(false);
    }//GEN-LAST:event_botonCambiarContraseñabotonMouseExited

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed


        String message = "";
        if (nameField.getText().equals("") || telField.getText().equals("") || passField.getText().equals("")) {
            if (nameField.getText().equals("")) {
                message += "El campo nombre no puede estar vacio \n";
            }
            if (passField.getText().equals("")) {
                message += "El campo password no puede estar vacio \n";
            }
            if (passField.getText().equals("")) {
                message += "El campo password no puede estar vacio \n";
            }
            if (telField.getText().equals("")) {
                message += "El campo telefono no puede estar vacio \n";
            }
            JOptionPane.showMessageDialog(this, message);
            return;
        }

        if (mod == 1) {
            empleado.setNombre(nameField.getText());
            empleado.setCargo((String) campoCargo.getSelectedItem());
            empleado.setTelefono(telField.getText());
            empleado.setCelular(celField.getText());
            DaoEmpleado.update(empleado);

        } else {

            String password = Encriptacion.md5(passField.getText());
            List<Empleado> empleados = new ArrayList<Empleado>();
            empleados = DaoEmpleado.readAll();

            if (passField.getText().length() < 4) {
                JOptionPane.showMessageDialog(this, "El password debe ser de 4 dígitos");
                return;
            }

            for (int i = 0; i < empleados.size(); i++) {
                if (empleados.get(i).getPassword().equals(password)) {
                    JOptionPane.showMessageDialog(this, "Ese password ya existe, escoja otro");
                    return;
                }
            }

            int id = DaoEmpleado.readAll().size() + 1;
            Empleado e = new Empleado(nameField.getText(), passField.getText(), (String) campoCargo.getSelectedItem(), telField.getText(), celField.getText());

            DaoEmpleado.create(e);
        }


        dispose();
    }//GEN-LAST:event_botonAceptarActionPerformed

    private void passFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passFieldActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_passFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DialogoCrearEmpleado dialog = new DialogoCrearEmpleado(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCambiarContraseña;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JComboBox campoCargo;
    private javax.swing.JTextField celField;
    private javax.swing.JLabel labelCargo;
    private javax.swing.JLabel labelCel;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelPass;
    private javax.swing.JLabel labelTel;
    private javax.swing.JTextField nameField;
    private javax.swing.JPanel panelInformacion;
    private javax.swing.JTextField passField;
    private javax.swing.JTextField telField;
    // End of variables declaration//GEN-END:variables
}
